package com.sservice.service.service.impl;

import com.sservice.common.Arith;
import com.sservice.model.dao.GoodsPriceIndexMapper;
import com.sservice.model.dao.IGoodsDao;
import com.sservice.model.dao.IOfferDao;
import com.sservice.model.dao.OfferPriceLogMapper;
import com.sservice.model.entity.Goods;
import com.sservice.model.entity.GoodsPriceIndex;
import com.sservice.model.entity.Offer;
import com.sservice.model.entity.OfferPriceLog;
import com.sservice.model.vo.GoodsPriceIndexVo;
import com.sservice.service.service.IGoodsPriceIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by suweiming on 2017/12/20.
 */


@Service
public class GoodsPriceIndexServiceImpl implements IGoodsPriceIndexService {

    @Autowired
    private GoodsPriceIndexMapper dao;
    @Autowired
    private IGoodsDao goodsdao;
    @Autowired
    private OfferPriceLogMapper oplMaper;
    @Autowired
    private IOfferDao offerDao;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(GoodsPriceIndex record) {
        return dao.insert(record);
    }

    @Override
    public int insertSelective(GoodsPriceIndex record) {
        return dao.insertSelective(record);
    }

    @Override
    public GoodsPriceIndex selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsPriceIndex> queryByPageAndGoodsId(GoodsPriceIndexVo gpiv) {
        return dao.queryByPageAndGoodsId(gpiv);
    }

    @Override
    public int updateByPrimaryKeySelective(GoodsPriceIndex record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(GoodsPriceIndex record) {
        return dao.updateByPrimaryKey(record);
    }

    public int selectTodayByGoodsId(String goodsId) {
        GoodsPriceIndex record = new GoodsPriceIndex();
        record.setGoodsId(goodsId);
        GoodsPriceIndex gpi = dao.selectBy(record);
        if (gpi == null) {
            return 0;
        }
        return gpi.getId();
    }

    ;

    //SELECT * from goods_price_index where to_days(create_at) = to_days('2017-04-26 09:42:30')
    @Override
    public int updateAvgByGoodsId(int goodsId) throws IllegalAccessException {
        Goods g = goodsdao.selectByPrimaryKey(goodsId);
        offerDao.queryOfferAvgPriceByGoodsId(String.valueOf(g.getId()));
        int r = selectTodayByGoodsId(String.valueOf(goodsId));
        if (r == 0) {
            GoodsPriceIndex gpi = new GoodsPriceIndex();
            gpi.setGoodsId(String.valueOf(goodsId));
            if (g.getOnline().equals("0")) {
                gpi.setPrice(0.0);
            } else {
                List<OfferPriceLog> list = oplMaper.selectTodayOnlineOfferChangeListByGoodsId(String.valueOf(goodsId));
                List<Offer> list2 = offerDao.queryOfferListByGoodsId(String.valueOf(goodsId));
                double avg_ = avg(list, list2);
                gpi.setPrice(avg_);
            }
            dao.insertSelective(gpi);
        } else {
            GoodsPriceIndex gpi = new GoodsPriceIndex();
            gpi.setGoodsId(String.valueOf(goodsId));
            gpi.setId(r);
            if (g.getOnline().equals("0")) {
                gpi.setPrice(0.0);
            } else {
                List<OfferPriceLog> list = oplMaper.selectTodayOnlineOfferChangeListByGoodsId(String.valueOf(goodsId));
                List<Offer> list2 = offerDao.queryOfferListByGoodsId(String.valueOf(goodsId));
                double avg_ = avg(list, list2);
                gpi.setPrice(avg_);
            }
            dao.updateByPrimaryKeySelective(gpi);
        }
        return 0;
    }

    private double avg(List<OfferPriceLog> list, List<Offer> list2) throws IllegalAccessException {
        double total = 0;
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            OfferPriceLog opl = list.get(i);
            total = Arith.add(opl.getPrice(), total);
            index++;
        }
        for (int i = 0; i < list2.size(); i++) {
            Offer opl = list2.get(i);
            total = Arith.add(opl.getPrice(), total);
            index++;
        }
        if (index == 0) return 0.0;
        return Arith.div(total, Double.valueOf(index));
    }
}
